define([
    "app"
],function (app) {
   app.controller("selectSeatRoomController",[
       "$scope",
       "$stateParams",
       "selectSeatDataService",
       "utilService",
       function ($scope,$stateParams,selectSeatDataService,utilService) {
           selectSeatDataService
               .getAllSeatsInfo($stateParams.roomNo)
               .then(
                   function success(resp) {

                       var currentRoomNo = selectSeatDataService.currentRoomNo.data = parseInt($stateParams.roomNo);

                       $scope.seats = resp.data;
                       $scope.currentTeam = selectSeatDataService.currentTeam;
                       // 已经选择的座位
                       // 每次切换房间都要清空选择
                       selectSeatDataService.data = [];

                       // 座位按照座位号从低到高排序
                       $scope.seats.sort(function (e1, e2) {
                           return e1.seatNo - e2.seatNo;
                       });

                       /**
                        * 设置座位的初始状态
                        * @param {string} status 代表状态的字符串
                        * @param {number} index 座位在数组中的标号
                        */
                       function setSeatStatus(status, index) {
                           for(var property in $scope.seats[index].status) {
                               $scope.seats[index].status[property] = false;
                           }
                           $scope.seats[index].status[status] = true;
                       }

                       // 设置座位的初始状态
                       $scope.seats.forEach(function (e, i, arr) {
                           e.status = {
                               unselected:false,
                               selected:false,
                               disabled:false,
                               highlight:false,
                               conflict:false
                           };

                           e.member.length > 0 ? setSeatStatus('disabled',i) : setSeatStatus('unselected',i);
                       });


                       // 监听当前选择的团队
                       $scope.$watch("currentTeam.data",function (newVal) {
                           if(utilService.isObjectEmpty(newVal)) {return}

                           $scope.seats
                               // 选出所有已经被使用的座位
                               .filter(function (e) {
                               return e.status.disabled || e.status.highlight || e.status.conflict;
                           })
                               // 将座位状态设为默认的disabled(不可选)状态
                               .forEach(function (e) {
                               setSeatStatus('disabled', e.seatNo - 1);
                           });

                          newVal.members.forEach(function(e,i,arr) {
                             if(!e.seat) {return}
                             if(e.seat.roomNo !== currentRoomNo) {return}

                             // 设置座位状态
                             setSeatStatus(e.belongTeamNumber > 1 ? 'conflict' : 'highlight',e.seat.seatNo - 1);

                          });

                       });

                       // 设置选择的顺序
                       function setOrder() {
                           selectSeatDataService.seatSelected.data.forEach(function(e,i,arr) {
                               e.order = i + 1;
                           });
                       }

                       // 更改鼠标移过的座位
                       $scope.changeHoveredSeat = function (seat) {
                           selectSeatDataService.hoveredSeat.data = seat;
                       };

                       // 选择该座位
                       $scope.selectSeat = function (seat) {
                           console.log(seat);
                           selectSeatDataService.seatSelected.data.push(seat);
                           setSeatStatus('selected',seat.seatNo - 1);
                           setOrder();
                       };

                       // 取消选择该座位
                       $scope.unselectSeat = function (seat) {
                           var arr = selectSeatDataService.seatSelected.data;
                           arr.splice(arr.lastIndexOf(seat),1);
                           seat.order = null;
                           setSeatStatus('unselected',seat.seatNo - 1);
                           setOrder();
                       };

                   },
                   function error(resp) {

                   }
               )
       }
   ])
});